var entityCollection = require('../../../Base/server/controllers/EntityCollection');
var publicController = require('../../../Base/server/controllers/Base.server.controller');
var appEnum = require('../../../App/server/controllers/Enum.server.controller');
var fs = require('fs');
var publicConst = require("../../../Base/server/controllers/Const");

exports.getApps = function (req, res) {
    var parameter = req.body;
    //var enterpriseID = parameter.EnterpriseID;
    var type = parameter.Type;
    
    var entity = entityCollection.getEntity("App");

    entity.find({IsEffective: true,Type: type}, null, null, function(err, records){
        if(err){
            res.json({
                Error: err
            });
        }
        else{
            res.json({
                Data: records
            });
        }
    });
};

exports.saveDownloadTimes = function (req, res) {
    var parameter = req.body;

    var entity = entityCollection.getEntity("App");

    // entity.Entity.findOneAndUpdate({
    //     Type: appEnum.DeviceType.SmartPlant.Value
    // }, {
    //     $inc: {
    //         Times: 1
    //     }
    // }, {
    //     upsert: true,
    //     setDefaultsOnInsert: true
    // }).then(function () {
    //     res.json({
    //         Data: true
    //     });
    // }, function (err) {
    //     res.json({
    //         Error: err
    //     });
    // });

    entity.Entity.findOneAndUpdate({
        Type: appEnum.DeviceType.SmartPlant.Value
    }, {
        $inc: {
            Times: 1
        }
    }, {
        upsert: true,
        setDefaultsOnInsert: true
    },function(err, records){
        if(err){
            res.json({
                Error: err
            });
        }
        else{
            res.json({
                Data: true
            });
        }
    });
    // entity.updateByValue({
    //     Type: appEnum.DeviceType.SmartPlant.Value
    // },
    // {
    //     $inc: {
    //         Times: 1
    //     }
    // }, function(err, records){
    //     if(err){
    //         res.json({
    //             Error: err
    //         });
    //     }
    //     else{
    //         res.json({
    //             Data: true
    //         });
    //     }
    // });
};

//Get方式获取文件流
exports.getFileStreamByGet = function(req, res) {
    var fileName = req.params.FileName;
    var stream = fs.createReadStream(publicConst.FilePath + fileName);
    res.set('Content-Type', 'application/vnd.android.package-archive');
    stream.pipe(res);
};

//获取文件流
exports.getFileStream = function (req, res) {
    publicController.getFileStream(req, res);
};

//获取共享文件信息
exports.getSharedFileInfo = function(req, res) {
    var query;
    var parameter = req.body;
    var type = parameter.Type;
    if(type){
        query = {
           IsEffective: true,
           Type: type
       }
    }else{
        query = {
           IsEffective: true
        }
    }
    var entity = entityCollection.getEntity("App");

    entity.find(query, {
        Name: 1
    }, null, function(err, records) {
        if (err) {
            res.json({
                Data: null,
                Error: err
            });
        } else {
            res.json({
                Data: records,
                Error: null
            });
        }
    });
};
